import { Injectable } from '@nestjs/common'
// import { CreateOrderDto } from './dto/create-order.dto'
import { UpdateOrderDto } from './dto/update-order.dto'
import { PrismaService } from '../prisma/prisma.service'
import { Redis } from 'ioredis'
import { InjectRedis } from '@nestjs-modules/ioredis'

@Injectable()
export class OrderService {
  constructor(
    @InjectRedis() private readonly redis: Redis,
    private prisma: PrismaService,
  ) {}

  async create() {
    const token = await this.redis.set('token', 'abc123')
    return {
      data: {
        token,
      },
    }
  }

  async findAll() {
    // await this.redis.rpush('order:arr', 'a', 'b', 'c')
    // const data = await this.redis.lrange('order:arr', 0, -1)
    const token = await this.redis.get('token')
    if (token) {
      return token
    }
    return '没有token'
  }

  async findOne(id: number) {
    return await this.prisma.order.findUnique({
      where: {
        id,
      },
    })
  }

  async update(id: number, updateOrderDto: UpdateOrderDto) {
    return await this.prisma.order.update({
      where: {
        id,
      },
      data: updateOrderDto,
    })
  }

  async remove(id: number) {
    return await this.prisma.order.delete({
      where: {
        id,
      },
    })
  }
}
